package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Prescription;
import com.ruoyi.system.service.IPrescriptionService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 处方Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "处方模块")
@RestController
@RequestMapping("/system/Prescription")
public class PrescriptionController extends BaseController {
    @Autowired
    private IPrescriptionService prescriptionService;
    private class Wrap implements Serializable {
        Prescription Prescription;
        String UserId;
        @JsonProperty("UserId")
        public String getUserId() {
            return UserId;
        }
        @JsonProperty("Prescription")
        public Prescription getPrescription() {
            return Prescription;
        }

    }
    @Autowired
    SysUserServiceImpl sysUserService;

    private Wrap convert(Prescription origin) {
        Wrap tempWrap = new Wrap();
        tempWrap.UserId = sysUserService.selectUserById(origin.getUserId()).getUserName();
        tempWrap.Prescription = origin;
        return tempWrap;
    }
    /**
     * 查询处方列表
     */
    @ApiOperation(value = "查询处方列表")
    @PreAuthorize("@ss.hasPermi('system:Prescription:list')")
    @GetMapping("/list")
    public Page<Wrap> list(Prescription entity, Page<Prescription> page) {
        LambdaQueryWrapper<Prescription> lambdaQueryWrapper = new LambdaQueryWrapper<Prescription>();
        Page<Prescription> old=prescriptionService.page(page, lambdaQueryWrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps=new ArrayList<>();
        List<Prescription> object=old.getRecords();
        for ( Prescription item : object) {
            wraps.add(convert(item));
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil=new PageUtil();
        pageUtil.setPage(pageWrap,old);
        return pageWrap;
    }

    /**
     * 导出处方列表
     */
    @ApiOperation(value = "导出处方列表")
    @PreAuthorize("@ss.hasPermi('system:Prescription:export')")
    @Log(title = "处方", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Prescription prescription) {
        LambdaQueryWrapper<Prescription> queryWrapper = new LambdaQueryWrapper<>();
        List<Prescription> list = prescriptionService.list(queryWrapper);
        ExcelUtil<Prescription> util = new ExcelUtil<Prescription>(Prescription.class);
        util.exportExcel(response, list, "处方数据");
    }


    /**
     * 获取处方详细信息
     */
    @ApiOperation(value = "获取处方详细信息")
    @PreAuthorize("@ss.hasPermi('system:Prescription:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(prescriptionService.getById(id));
    }

    /**
     * 新增处方
     */
    @ApiOperation(value = "新增处方")
    @Log(title = "处方", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prescription prescription) {
        if(prescriptionService.save(prescription)){
            return AjaxResult.success(prescription.getId());
        }
        return AjaxResult.error();
    }

    /**
     * 修改处方
     */
    @ApiOperation(value = "修改处方")
    @PreAuthorize("@ss.hasPermi('system:Prescription:edit')")
    @Log(title = "处方", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prescription prescription) {
        return toAjax(prescriptionService.updateById(prescription));
    }


    /**
     * 删除处方
     */
    @ApiOperation(value = "删除处方")
    @PreAuthorize("@ss.hasPermi('system:Prescription:remove')")
    @Log(title = "处方", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(prescriptionService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
